﻿//-----------------------------------------------------------------------
// <copyright file="SmallMessageFormatter.cs" company="AFS">
// Copyright (c) AFS. All rights reserved.
//
//    This file is part of Chatbox.
//
//    Chatbox is free software: you can redistribute it and/or modify
//    it under the terms of the GNU Lesser General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    any later version.
//
//    Chatbox is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//    GNU Lesser General Public License for more details.
//
//    You should have received a copy of the GNU Lesser General Public License
//    along with Chatbox. If not, see http://www.gnu.org/copyleft/lesser.html
// </copyright>
//----------------------------------------------------------------------- 

namespace ChatBox.UI.MessageDisplay
{
    using System.Windows.Documents;
    using ChatBox.DomainObjects.XmlFeedObjects;
    using ChatBox.Interfaces;
    using ChatBox.XamlUtility;

    /// <summary>
    /// Format a message in a small font
    /// </summary>
    public class SmallMessageFormatter : IMessageFormatter
    {
        /// <summary>
        /// Gets or sets the HyperlinkContext
        /// </summary>
        public HyperlinkContext HyperlinkContext { get; set; }

        /// <summary>
        /// Gets or sets the error log
        /// </summary>
        public ILogger Logger { get; set; }
        
        /// <summary>
        /// Format the message
        /// </summary>
        /// <param name="message">the message to format</param>
        /// <param name="paragraph">the paragraph to format the message into</param>
        /// <param name="formatOptions">Formatting options</param>
        public void FormatMessageIntoParagraph(Message message, Paragraph paragraph, FormatOptions formatOptions)
        {
            double actualFontSize = paragraph.FontSize * HtmlToXamlConverter.SmallFontScale;

            Span contentSpan = new Span();
            contentSpan.FontSize = actualFontSize;

            if (formatOptions.ShowDate != DateTimeDisplay.None)
            {
                contentSpan.Inlines.Add(MessageDisplayHelpers.DateFormat(message, formatOptions.ShowDate));
            }

            if (message.Group != null)
            {
                paragraph.Inlines.Add(MessageDisplayHelpers.GroupFrom(message, this.HyperlinkContext, formatOptions));
            }

            contentSpan.Inlines.Add(MessageDisplayHelpers.SenderFormat(message, this.HyperlinkContext));
            if (formatOptions.ShowSendPrivateMessageButton)
            {
                contentSpan.Inlines.Add(MessageDisplayHelpers.PrivateMessageToUserButton(message.From, formatOptions));
            } 
            
            contentSpan.Inlines.Add(new Run(" "));
            contentSpan.Inlines.Add(HtmlToXamlConverter.Convert(message.MessageText, this.HyperlinkContext, this.Logger, null));

            paragraph.Inlines.Add(contentSpan);
        }
    }
}